package com.trinity.order.refund.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trinity.common.utils.DateUtils;
import com.trinity.order.refund.domain.XcRefundOrder;
import com.trinity.order.refund.mapper.XcRefundOrderMapper;
import com.trinity.order.refund.service.IXcRefundOrderService;
import com.trinity.orders.constant.OrderConstants;
import com.trinity.orders.mapper.XcOrdersMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
import java.util.List;

/**
 * 退款订单Service业务层处理
 *
 * @author 董诗攀
 * @date 2024-02-26
 */
@Service
public class XcRefundOrderServiceImpl extends ServiceImpl<XcRefundOrderMapper, XcRefundOrder> implements IXcRefundOrderService {
    @Resource
    private XcRefundOrderMapper xcRefundOrderMapper;

    /**
     * 查询退款订单
     *
     * @param id 退款订单主键
     * @return 退款订单
     */
    @Override
    public XcRefundOrder selectXcRefundOrderById(Long id) {
        return xcRefundOrderMapper.selectXcRefundOrderById(id);
    }

    /**
     * 查询退款订单列表
     *
     * @param xcRefundOrder 退款订单
     * @return 退款订单
     */
    @Override
    public List<XcRefundOrder> selectXcRefundOrderList(XcRefundOrder xcRefundOrder) {
        return xcRefundOrderMapper.selectXcRefundOrderList(xcRefundOrder);
    }

    /**
     * 新增退款订单
     *
     * @param xcRefundOrder 退款订单
     * @return 结果
     */
    @Override
    public int insertXcRefundOrder(XcRefundOrder xcRefundOrder) {
        xcRefundOrder.setCreateTime(DateUtils.getNowDate());
        return xcRefundOrderMapper.insertXcRefundOrder(xcRefundOrder);
    }

    /**
     * 修改退款订单
     *
     * @param xcRefundOrder 退款订单
     * @return 结果
     */
    @Override
    public int updateXcRefundOrder(XcRefundOrder xcRefundOrder) {
        xcRefundOrder.setUpdateTime(DateUtils.getNowDate());
        return xcRefundOrderMapper.updateById(xcRefundOrder);
    }

    /**
     * 批量删除退款订单
     *
     * @param ids 需要删除的退款订单主键
     * @return 结果
     */
    @Override
    public int deleteXcRefundOrderByIds(Long[] ids) {
        return xcRefundOrderMapper.deleteXcRefundOrderByIds(ids);
    }

    /**
     * 删除退款订单信息
     *
     * @param id 退款订单主键
     * @return 结果
     */
    @Override
    public int deleteXcRefundOrderById(Long id) {
        return xcRefundOrderMapper.deleteXcRefundOrderById(id);
    }

    @Override
    public List<XcRefundOrder> selectRefundOrderDuration(int minutes) {
        //minutes 分钟之前的时间
        Instant instant = Instant.now().minus(Duration.ofMillis(minutes));
        QueryWrapper<XcRefundOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", OrderConstants.RefundState.PROCESSING);
        queryWrapper.le("create_time", instant);
        List<XcRefundOrder> refundOrders = xcRefundOrderMapper.selectList(queryWrapper);
        return refundOrders;
    }

    @Override
    public void closeRefundNotExit(String id, String transactionId, String outTradeNo, String outRefundNo, String refundId) {
        XcRefundOrder xcRefundOrder = new XcRefundOrder();
        xcRefundOrder.setStatus(OrderConstants.RefundState.CLOSED);
        xcRefundOrder.setRemark("退款单不存在!");

        if (id != null && !id.isEmpty()) {
            xcRefundOrder.setId(Long.valueOf(id));
            xcRefundOrderMapper.updateById(xcRefundOrder);
        }
        UpdateWrapper<XcRefundOrder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("refund_id", refundId); // 添加条件，例如基于主键的条件
        xcRefundOrderMapper.update(xcRefundOrder, updateWrapper);
    }

    @Override
    public XcRefundOrder selectRefundOrderByRefundNO(String outRefundNo) {
        QueryWrapper<XcRefundOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("out_refund_no", outRefundNo);
        return xcRefundOrderMapper.selectOne(queryWrapper);
    }

    @Override
    public List<XcRefundOrder> selectRefundOrderListByOrderId(String id) {
        QueryWrapper<XcRefundOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", id)
                .eq("status", OrderConstants.RefundState.SUCCESS);
        return xcRefundOrderMapper.selectList(queryWrapper);
    }
}
